package com.gpj.mini.tools.fr;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @className DirScan
 * @description
 *   TODO
 * @author GPJ
 * @date 2021/6/17 17:09
 * @version 1.0
 **/
@Slf4j
@AllArgsConstructor
public class DirScan {
	private String dirPath;
	private String suffix;

	public List<String> scan(){
		List<String> paths = new ArrayList<>();
		doScan(new File(dirPath), paths);
		return paths;
	}

	private void doScan(File dir, List<String> list){
		if(!dir.exists()){
			log.warn("路劲[{}]不存在", dir.getPath());
			return;
		}else if (!dir.isDirectory()) {
			String name = dir.getName();
			if(StringUtils.isBlank(suffix) || name.endsWith(suffix)){
				list.add(dir.getPath());
			}
			return;
		}

		for (File file : dir.listFiles()) {
			doScan(file, list);
		}
	}
}
